<?php

namespace app\common\model\logic;

class GroupModel
{
    public function __construct()
    {
        $this->curModel = model('dao.group');
    }

    public function getList($data)
    {
    }

    //保存添加
    public function saveAdd($data)
    {
        $data['status'] = 1;

        return $this->curModel->allowField(true)->isUpdate(false)->save($data);
    }

    //保存编辑
    public function saveEdit($data)
    {
        $this->getRow($data);

        return $this->curModel->allowField(true)->isUpdate(true)->save($data);
    }

    //保存删除
    public function saveDel($data)
    {
        $this->getRow($data);
        if ($this->getChildrenSum($data['id'], 0) > 0) {
            exception('存在子权限组,不能删除', 0, 'app\\common\\exception\\LogicException');
        }
        $result = $this->curModel->destroy($data['id']);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('DEL_ERROR'), 0, 'app\\common\\exception\\LogicException');
        }
    }

    public function getChildrenAll($parentId, $data)
    {
        $sum = $this->getChildrenSum($parentId, $data);
        if ($sum > 0) {
            $where = $this->getTreeWhere($parentId, $data);
            $menuList = $this->curModel->where($where)->all()->toArray();
            $bind = model('server.auth_apis_bind');
            $apis = model('server.auth_apis');
            foreach ($menuList as &$ve) {
                $ve['title'] = $ve['name'];
                $ve['class_type'] = 'group';
                $childSum = $sum = $this->getChildrenSum($ve['id'], $data);
                if ($childSum > 0) {
                    $ve['children'] = $this->getChildrenAll($ve['id'], $data);
                } else {
                    $ve['children'] = [];
                }

                if (isset($data['type']) && $data['type'] == 2) {
                    $bindwhere['target_type'] = 2;
                    $bindwhere['id_list'] = $ve['id'];
                    $bindList = $bind->getApisAll($bindwhere);
                    if (!empty($bindList)) {
                        foreach ($bindList as $vr) {
                            $apisWhere['id'] = $vr['apis_id'];
                            $apiRow = $apis->getLine($apisWhere);
                            $row['id'] = $apiRow['id'];
                            $row['class_type'] = 'node';
                            $row['title'] = $apiRow['module'].'_'.$apiRow['controller'].'_'.$apiRow['action'];
                            $ve['children'][] = $row;
                        }
                    }
                }
            }

            return $menuList;
        } else {
            return [];
        }
    }

    //获取权限
    public function getPower($data)
    {
        $bind = model('server.auth_apis_bind');

        return  $bind->getGroupPower($data);
    }

    //保存权限
    public function savePower($data)
    {
        $bind = model('server.auth_apis_bind');

        return $bind->saveGroupPower($data);
    }

    public function getChildrenSum($parentId, $data)
    {
        $where = $this->getTreeWhere($parentId, $data);

        return $this->curModel->where($where)->count();
    }

    //获取单行
    public function getRow($data)
    {
        $where[] = ['id', '=', $data['id']];

        $line = $this->getLine($where);
        if (empty($line)) {
            exception('权限组不存在,请核实后再试', 0, 'app\\common\\exception\\DataEmptyException');
        } else {
            return $line;
        }
    }

    //根据条件获取单行
    public function getLine($where)
    {
        return  $this->curModel->where($where)->find();
    }

    private function getTreeWhere($parentId, $data)
    {
        $where[] = ['parent_id', '=', $parentId];
        if (isset($data['status']) && !empty($data['status'])) {
            $where[] = ['status', '=', $data['status']];
        }

        return $where;
    }
}
